<%@ page import="br.com.rosena.model.bean.persistente.*" %>
<%@ page import="br.com.rosena.control.ControllerRosena" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="br.com.rosena.view.gui.actions.ACFRMLoginPrincipal" %>
<%
    String nomeClasseR = request.getParameter("nomeClasse") != null ? request.getParameter("nomeClasse") : "";
    String propriedadeInversaR = request.getParameter("propriedadeInversa");
    Persistente objPersistenteR = new Persistente(nomeClasseR);
    if (nomeClasseR.equals("")) {
        throw new Exception("Nao foi possivel carregar a tela");
    }
    Classe objClasseR = objPersistenteR.getClasse();
    Atributo lstAtributoR[] = objClasseR.getArrayAtributos();
    Metodo lstMetodoR[] = objClasseR.getArrayComando();
    br.com.rosena.util.Util.bubbleSortMetodo(lstMetodoR);
    br.com.rosena.util.Util.bubbleSortAtributo(lstAtributoR);
    ACFRMLoginPrincipal.objUsuarioLogado = new Persistente("Usuario",1);

%>
<form id="frm<%=nomeClasseR%>">
    <div class="tab-page" id="tabPage1">
        <fieldset>
            <legend><%=nomeClasseR%></legend>
            <table class="Tabela01" cellpadding="0" cellspacing="0" >
                <tr>
                    <td width="12.5%"></td>
                    <td width="12.5%"></td>
                    <td width="12.5%"></td>
                    <td width="12.5%"></td>
                    <td width="12.5%"></td>
                    <td width="12.5%"></td>
                    <td width="12.5%"></td>
                    <td width="12.5%"></td>
                </tr>
                <tr><td colspan="8"><br></td></tr>
                <%double colspan = 0;
                    boolean entrar = false;
                    ControllerRosena controleR = new ControllerRosena();
                    for(int i=0;i<lstAtributoR.length;i++){
                        Atributo atributo = lstAtributoR[i];
                        String tipoAtributo = atributo.getTipo();
                        if(atributo.getNome().equals(propriedadeInversaR) ){
                            continue;
                        }
                        colspan += atributo.getTamanhoLargura();
                        if(colspan > 8 || i == 0 || entrar){%>
                            <tr>
                            <%entrar=false;%>
                        <%}%>
                        <%if(tipoAtributo.contains(Atributo.XML)){%>
                            <%if(!atributo.getConsultaDoAtributo().equals(Atributo.XML_LIST) ){%>
                                <td colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                    <%=atributo.isObrigatorio()  && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                                    <select name="<%=atributo.getNome()%>" onchange="obj<%=nomeClasseR%>.setPersistente('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%>').value);" class="Combo01" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>>
                                        <option value="">Selecione uma opcao</option>
                                        <%  Persistente objPersistenteInterno = getPersistente(objPersistenteR,atributo.getNome().trim().toUpperCase() );
                                            Consulta consulta = objPersistenteInterno.getClasse().getConsulta(atributo.getConsultaDoAtributo());
                                            ResultSet crsAtributo = controleR.execultarQueryReturResultSet(consulta.getSql());
                                            while (crsAtributo.next()) {%>
                                                <%if( objPersistenteR.getPersistente(atributo.getNome()).getId()  == crsAtributo.getLong("id_objeto") ){%>
                                                    <option  selected="selected" value="<%=crsAtributo.getString("id_objeto")%>"><%=crsAtributo.getString("descricao_objeto")%></option>
                                                <%}else{%>
                                                    <option value="<%=crsAtributo.getString("id_objeto")%>"><%=crsAtributo.getString("descricao_objeto")%></option>
                                                <%}%>
                                         <%}%>
                                    </select>
                                </td>
                            <%}%>
                        <%}else if(tipoAtributo.equals(Atributo.String)){%>
                                <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                    <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributo.getNome()%>" name="<%=atributo.getNome()%>" onchange="obj<%=nomeClasseR%>.setString('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=objPersistenteR.getString(atributo.getNome())%>">
                                </td>
                        <%}else if(tipoAtributo.equals(Atributo.Boolean)){%>
                                <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                    <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributo.getNome()%>"  name="<%=atributo.getNome()%>" onchange="obj<%=nomeClasseR%>.setBoolean('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%>').value);"  type="checkbox"  <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>    checked="<%=objPersistenteR.getBoolean(atributo.getNome())?"checked":""%>">
                                </td>
                        <%}else if(tipoAtributo.equals(Atributo.Double)){%>
                                <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                    <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributo.getNome()%>" name="<%=atributo.getNome()%>" onchange="obj<%=nomeClasseR%>.setDouble('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%>').value);"  type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%> onblur="formatarNumero(this,'0.00')"  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=objPersistenteR.getDouble(atributo.getNome())%>">
                                </td>
                        <%}else if(tipoAtributo.equals(Atributo.Integer)){%>
                                <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                    <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributo.getNome()%>" name="<%=atributo.getNome()%>" onchange="obj<%=nomeClasseR%>.setInt('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%> onblur="formatarNumero(this,'0')" class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=objPersistenteR.getInt(atributo.getNome())%>">
                                </td>
                        <%}else if(tipoAtributo.equals(Atributo.Date)){%>
                                <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                    <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributo.getNome()%>" name="<%=atributo.getNome()%>" onchange="obj<%=nomeClasseR%>.setDate('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=br.com.rosena.util.Util.formatarData(objPersistenteR.getDate(atributo.getNome()),"dd/MM/yyyy")%>">
                                </td>
                         <%}else if(tipoAtributo.equals(Atributo.Time)){%>
                                <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                    <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributo.getNome()%>" name="<%=atributo.getNome()%>" onchange="obj<%=nomeClasseR%>.setTime('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=br.com.rosena.util.Util.formatarData(objPersistenteR.getTime(atributo.getNome()),"HH:mm")%>">
                                </td>
                         <%}else if(tipoAtributo.equals(Atributo.TimeStamp)){%>
                                <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                    <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributo.getNome()%>" name="<%=atributo.getNome()%>" onchange="obj<%=nomeClasseR%>.setTime('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=br.com.rosena.util.Util.formatarData(objPersistenteR.getTime(atributo.getNome()),"dd/MM/yyyy HH:mm:ss")%>">
                                </td>
                         <%}%>
                        <%if(colspan >= 8){%>
                            </tr>
                            <%colspan=0;
                              entrar = true;%>
                        <%}%>
                    <%}%>
                <tr>
                    <td><br></td>
                </tr>
                <tr>
                    <td colspan="8">
                        <table  class="Tabela01" cellpadding="0" cellspacing="0" align="center" >
                            <tr align="center">
                                <td>
                                    <%for(int i=0;i<lstMetodoR.length;i++){
                                        Metodo metodo = lstMetodoR[i];%>
                                        <%if(metodo.getOrdem() != -1){%>
                                            <input class="Botao01" onclick="<%=metodo.getNome()%>_<%=nomeClasseR%>();" type="button" value="<%=metodo.getNomeUsual()%>">
                                        <%}%>
                                    <%}%>
                                     <input class="Botao01" onclick="limparCampos('<%=nomeClasseR%>',obj<%=nomeClasseR%>,'frm');" type="button" value="Limpar">
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr><td colspan="8"><div id="botoes"></div></td></tr>
            </table>
        </fieldset>
        <div id="localizar"></div>
    </div> 
</form>
<%!
    public Persistente getPersistente(Persistente objPersistenteObj,String nomePropriedade) {
        try {
            if (((Persistente) objPersistenteObj.getDadosObjeto().getHstValores().get(nomePropriedade.trim().toUpperCase())).getId() > 0) {
                return ((Persistente) objPersistenteObj.getDadosObjeto().getHstValores().get(nomePropriedade.trim().toUpperCase()));
            } else {
                return ((Persistente) objPersistenteObj.getDadosObjeto().getHstValores().get(nomePropriedade.trim().toUpperCase()));
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
%>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          